/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Jose Dominguez, Carlos E. Tejada, José M. Martínez
 */
public class SolveForX {

    private String rawInput;
    private Solver solver;
    private double[][] matrix;
    private OCR ocrObject;
    private BufferedImage bufferedImage;

    public SolveForX(String path) {
        File f = new File(path);
        try {
             bufferedImage = ImageIO.read(f);
        } catch (IOException ex) {
            Logger.getLogger(SolveForX.class.getName()).log(Level.SEVERE, null, ex);
        }
        ocrObject = new OCR(bufferedImage);
        rawInput = ocrObject.getText();
        System.out.println(rawInput);
        //interpretar();
    }

    private String interpretar() {
        String s = "";
        if (rawInput.charAt(1) == '[') {
            rawInput = rawInput.replace('[', '\0');
            rawInput = rawInput.replace(']', '\0');
            String[] temp1 = rawInput.split("\n");
            matrix = new double[temp1.length][temp1[0].split(" ").length];
            for (int i = 0; i < temp1.length; i++) {
                for (int j = 0; j < temp1[i].split(" ").length; j++) {
                    matrix[i][j] = Double.parseDouble(temp1[i].split(" ")[j]);
                }
            }
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    System.out.println(matrix[i][j]);
                }
            }
            solver = new Solver(matrix);
            double[][] resp = solver.matrix();
            for (int i = 0; i < resp.length; i++) {
                for (int j = 0; j < resp[i].length; j++) {
                    s += "" + resp[i][j] + ":";
                }
                s += "#";
            }
        } else if (rawInput.charAt(1) == '∫') {
            rawInput = rawInput.replace('∫', '\0');
            System.out.println(rawInput);
            solver = new Solver(rawInput);
            s = solver.integrate();
            solver.cerrar();
            System.out.println(s);

        } else if (rawInput.contains("dx/dy")) {
            rawInput = rawInput.substring(6);
            System.out.println(rawInput);
            solver = new Solver(rawInput);
            s = solver.deriviative();
            solver.cerrar();
            System.out.println(s);
        } else if (rawInput.charAt(1) == '{') {
            rawInput = rawInput.replace('{', '\0');
            rawInput = rawInput.replaceAll("[a-z]|[A-Z]", "1");
            rawInput = rawInput.replaceAll("[0-9][A-Z]|[a-z]", "");
            System.out.println(rawInput);
            String[] temp1 = rawInput.split("\n");
            Double results[] = new Double[temp1.length];
            /*
             * for (int i = 0; i < results.length; i++) { results[i] =
             * Double.parseDouble(temp1[i].split("=")[1]);
            }
             */
            temp1[0] = temp1[0].substring(2);
            for (String double1 : temp1) {
                System.out.println(double1);
            }

            for (String line : temp1) {
            }
        } else {
            rawInput = rawInput.replace('﻿', '\0');
            rawInput = rawInput.substring(1);
            System.out.println(rawInput);
            solver = new Solver(rawInput);
            s = solver.solveEquation();
            System.out.println(solver.solveEquation());
        }
        return s;
    }

    /*
     * private String leerArchi() { File archivo = null; archivo = new
     * File("../"+"Ecuacion De Prueba/Ecuacion.txt"); DataInputStream dis;
     * FileInputStream fis = null; BufferedReader br; try { fis = new
     * FileInputStream(archivo); } catch (FileNotFoundException ex) {
     * Logger.getLogger(SolveForX.class.getName()).log(Level.SEVERE, null, ex);
     * } dis = new DataInputStream(fis); br = new BufferedReader(new
     * InputStreamReader(dis)); String s = "", linea = null; try { while ((linea
     * = br.readLine()) != null) { s += linea + '\n'; } } catch (IOException ex)
     * { Logger.getLogger(SolveForX.class.getName()).log(Level.SEVERE, null,
     * ex); } return s;
    }
     */
}
